package com.hemerasolutions.v2trainer.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hemerasolutions.v2trainer.dao.ExceptionDao;
import com.hemerasolutions.v2trainer.dao.interfaces.LibraryDaoInterface;
import com.hemerasolutions.v2trainer.dao.interfaces.UserDaoInterface;
import com.hemerasolutions.v2trainer.model.exercise.Equipment;
import com.hemerasolutions.v2trainer.model.exercise.Exercise;
import com.hemerasolutions.v2trainer.model.exerciseGroup.ExerciseGroup;
import com.hemerasolutions.v2trainer.model.library.Library;
import com.hemerasolutions.v2trainer.model.modality.Modality;
import com.hemerasolutions.v2trainer.model.training.Training;
import com.hemerasolutions.v2trainer.model.users.Academy;
import com.hemerasolutions.v2trainer.model.users.Admin;
import com.hemerasolutions.v2trainer.model.users.Athlete;
import com.hemerasolutions.v2trainer.model.users.PersonalTrainer;
import com.hemerasolutions.v2trainer.model.users.Teacher;
import com.hemerasolutions.v2trainer.model.users.User;
import com.hemerasolutions.v2trainer.service.interfaces.LibraryServiceInterface;

@Service("libraryService")
public class LibraryService extends BaseService implements LibraryServiceInterface {

	private static final long serialVersionUID = 1L;

	@Autowired
	private LibraryDaoInterface libraryDao;

	@Autowired
	private UserDaoInterface userDao;
	
	public LibraryService() {
		super();
	}
	
	@Override
	@PostConstruct
	public void init(){
		
	}
	
	@Override
	public List<Modality> findModalitysInLibrarys( Map<String,Object> fields ){
		return libraryDao.searchModalitysOfLibrary( fields);
	}
	
	@Override
	public List<Equipment> findEquipmentsInLibrarys ( Map<String,Object> fields ){
		return libraryDao.searchEquipmentsOfLibrary( fields);
	}

	@Override
	public List<Exercise> findExercisesInLibrarys ( Map<String,Object> fields ){
		return libraryDao.searchExercisesOfLibrary( fields);
	}

	@Override
	public List<Training> findTrainingSystemsInLibrarys ( Map<String,Object> fields ){
		return libraryDao.searchTrainingSystemsOfLibrary( fields);
	}

	@Override
	public List<Training> findTrainingsInLibrarys ( Map<String,Object> fields ){
		return libraryDao.searchTrainingsOfLibrary( fields);
	}
	
	@Override
	public List<ExerciseGroup> findExerciseGroupsInLibrarys ( Map<String,Object> fields ){
		return libraryDao.searchExerciseGroupsOfLibrary( fields);
	}

	
	public List<Library> listUserLibrarys(String userName){
		User user = userDao.findByUserName(userName);
		return user.getLibrarys();
	}

	
	public List<Library> listUserTypeLibrarys(int userType){
		return libraryDao.listPublicAdminLibrarys(userType);
		
	}

	@Override
	public List<Library> copyPublicLibrarys() {

		return this.listUserTypeLibrarys(User.USERTYPE_ADMIN);
	}

	@Override
	public Library createLibrary(Library library) throws ExceptionService {
		try{
			return libraryDao.create(library);
		}catch(ExceptionDao e){
			throw new ExceptionService(e);
		}
	}

	@Override
	public Library saveLibrary(Library library) throws ExceptionService {
		try{
			return libraryDao.save(library);
		}catch(ExceptionDao e){
			throw new ExceptionService(e);
		}
	}

	@Override
	public void deleteLibrary(Library library) throws ExceptionService {
		try{
			libraryDao.delete(library);
		}catch(ExceptionDao e){
			throw new ExceptionService(e);
		}
		
	}
	
	@Override
	public List<Library> listPublicAdminLibrarys(){
		
		return libraryDao.listPublicAdminLibrarys(User.USERTYPE_ADMIN);
		
	}
	
	public Library findUtilization(Library library){

		// Fazer a consistencia quando as agendas estiverem prontas... Pois depende de exercicios nas agendas
		
		return null;
		
	}

	@Override
	public List<Library> readPublicLibrarys() {
		return libraryDao.listPublicLibrarys();
		
	}

	@Override
	public List<Library> readLibrarys(Academy academy) {

		return academy.getUser().getLibrarys();
	}

	@Override
	public List<Library> readLibrarys(Admin admin) {

		return admin.getUser().getLibrarys();
	}

	@Override
	public List<Library> readLibrarys(Athlete athlete) {
		
		List<Library> librarys = new ArrayList<Library>();
		
		for(Academy academy: athlete.getAcademys()){
			for(Library lib:academy.getUser().getLibrarys()){
				if(lib.getShareType() == Library.SHARE_TYPE_AFFLIATES){
					librarys.add(lib);
				}
			}
			for(Teacher teacher:academy.getTeachers()){
				for(Library lib:teacher.getUser().getLibrarys()){
					if(lib.getShareType() == Library.SHARE_TYPE_AFFLIATES){
						librarys.add(lib);
					}
				}
			}
		}
		
		for(PersonalTrainer pt: athlete.getPersonalTrainers()){
			for(Library lib:pt.getUser().getLibrarys()){
				if(lib.getShareType() == Library.SHARE_TYPE_AFFLIATES){
					librarys.add(lib);
				}
			}			
		}
		
		return librarys;
	}

	@Override
	public List<Library> readLibrarys(PersonalTrainer pt) {
		return pt.getUser().getLibrarys();
	}

	@Override
	public List<Library> readLibrarys(Teacher teacher) {

		List<Library> librarys = new ArrayList<Library>();
		
		
		for(Library lib:teacher.getAcademy().getUser().getLibrarys()){
			if( lib.getShareType() == Library.SHARE_TYPE_AFFLIATES ||
				lib.getShareType() == Library.SHARE_TYPE_PERSONAL	){
				librarys.add(lib);
			}
		}
		for(Teacher teacher2:teacher.getAcademy().getTeachers()){
			if(teacher.getUser().getEmail() != teacher2.getUser().getEmail()){
				for(Library lib:teacher2.getUser().getLibrarys()){
					if(lib.getShareType() == Library.SHARE_TYPE_AFFLIATES){
						librarys.add(lib);
					}
				}
			}
		}
		
		librarys.addAll(teacher.getUser().getLibrarys());
		
		return librarys;
	
	}
	
}
